package com.zzy.demo.config;

import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.springframework.context.ApplicationContext;
import org.springframework.security.web.context.support.SecurityWebApplicationContextUtils;
import org.springframework.security.web.session.HttpSessionCreatedEvent;
import org.springframework.security.web.session.HttpSessionDestroyedEvent;

@WebListener
public class MySessionListener implements HttpSessionListener {
	
	public ApplicationContext getContext(ServletContext servletContext) {
		return SecurityWebApplicationContextUtils.findRequiredWebApplicationContext(servletContext);
	}
	
	@Override
	public void sessionCreated(HttpSessionEvent event) {
		System.out.println("Session创建-----------" + event.getSession().getId());
		HttpSessionCreatedEvent e =new HttpSessionCreatedEvent(event.getSession());
		this.getContext(event.getSession().getServletContext()).publishEvent(e);
	}
	@Override
	public void sessionDestroyed(HttpSessionEvent event) {
		System.out.println("Session销毁------------" + event.getSession().getId());
		HttpSessionDestroyedEvent e =new HttpSessionDestroyedEvent(event.getSession());
		this.getContext(event.getSession().getServletContext()).publishEvent(e);
	}
}
